#include "Glow.h"
#include <iostream>
#include <cstdlib>

using namespace std;
using namespace glow;

void drawPlane()
{
	glColor3ub(150, 150, 150);
	glPushMatrix();
	glTranslated(0, -4, 0);
	glScalef(20.0, 20.0, 20.0);
	glBegin(GL_QUADS);
	glVertex3d(-1, 0, -1);
	glVertex3d(-1, 0, 1);
	glVertex3d(1, 0, 1);
	glVertex3d(1, 0, -1);
	glEnd();
	glPopMatrix();
	glColor3ub(255, 255, 255);
}

int main(int argc, char **argv)
{
	int width = 800;
	int height = 600;
	bool fullscreen = false;
	if(argc >= 3)
	{
		width = atoi(argv[1]);
		height = atoi(argv[2]);
	}
	if(argc >= 4)
		fullscreen = true;
		
	// INITIALISATION
	GlowDevice *device = GlowDevice::getInstance();
	device->init(width, height, fullscreen);
	
	Renderer *rdr = device->getRenderer();
	EventManager *emgr = device->getEventManager();
	
	SkyBox sb;
	sb.load("../media/skybox/interZP.png",
		"../media/skybox/interZN.png",
		"../media/skybox/interYP.png",
		"../media/skybox/interYN.png",
		"../media/skybox/interXP.png",
		"../media/skybox/interXN.png");
	
	// Cameras
	Camera c1(Vector3d(15, 15, 15));
	c1.setSensivity(1.0);
	c1.setSpeed(2.0);
	
	// CHARGEMENT
	Mesh *tric = MeshFactory::create("../media/triceratops.off");
	tric->genNormals();
	tric->setAmbient(Color4f(0.0, 0.0, 0.0));
	tric->setSpecular(Color4f(0.1, 0.1, 0.1, 1.0));
	tric->setDiffuse(Color4f(1.0, 1.0, 1.0, 1.0));
	tric->setShininess(1.0);
	
	Light l1(GL_LIGHT0);
	l1.setPosition(Vector3f(0, 10, 0));
	l1.setDiffuse(Color4f(1.0, 1.0, 1.0, 1.0));

	bool quit = false;
	bool show = false;
	device->showCursor(show);
	
	// MAIN LOOP
	while(!emgr->quit() && !quit)
	{
		emgr->getEvent();
		c1.getEvent();
		c1.animate();
		rdr->beginScene();
		
		// Rendu multi-passe
		// Passe 1 (côté haut-gauche)
		rdr->pushMatrix();
		
		rdr->setViewport(0, 0, width/2, height/2);
		sb.setPosition(c1.getPosition());
		c1.look();
		sb.draw();
		drawPlane();
		l1.on();
		tric->draw();
		l1.off();
		
		rdr->popMatrix();
		
		// Passe 2 (côté haut-droite)
		rdr->pushMatrix();
		
		rdr->setViewport(width/2, 0, width/2, height/2);
		sb.setPosition(c1.getPosition());
		c1.look();
		sb.draw();
		drawPlane();
		rdr->setPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
		tric->draw();
		
		rdr->popMatrix();
		
		// Passe 3 (côté bas-gauche)
		rdr->pushMatrix();
		
		rdr->setViewport(0, height/2, width/2, height/2);
		sb.setPosition(c1.getPosition());
		c1.look();
		rdr->setPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
		sb.draw();
		drawPlane();
		rdr->setPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
		glColor3ub(255, 0, 0);
		tric->draw();
		glColor3ub(255, 255, 255);
		
		rdr->popMatrix();
		
		// Passe 4 (côté bas-droite)
		rdr->pushMatrix();
		
		rdr->setViewport(width/2, height/2, width/2, height/2);
		sb.setPosition(c1.getPosition());
		c1.look();
		rdr->setPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
		sb.draw();
		drawPlane();
		tric->draw();
		
		rdr->popMatrix();
		
		rdr->endScene();

		if(emgr->isKeyDownOnce(GLFW_KEY_ESC)) quit = true;
		if(emgr->isKeyDownOnce('B'))
		{
			show = !show;
			device->showCursor(show);
		}
	}

	delete tric;
	GlowDevice::destroy();
	
	return 0;
}
